﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 快速打开程序
{
    public partial class Form2 : Form
    {
        private StreamWriter _wstream;
        private StreamReader _rstream = null;

        public Form2()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 强推
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string path;
            int id = listBox1.SelectedIndex;
            if (id != -1 && id != 0)
            {
                path = listBox1.Items[listBox1.SelectedIndex].ToString();
            }
            else
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "选择文件夹"; ;//左上角提示
                path = string.Empty;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    path = dialog.SelectedPath;//获取选中文件路径
                }
            }
            int count = listBox1.Items.Count;
            int j = 0;
            for (int i = 0; i < count; i++)
            {

                if (listBox1.Items[i].ToString() == path)
                    j++;

            }
            if (j == 0)
                listBox1.Items.Add(path);
            string filePath = "Record.txt"; // 文件将写入到应用程序的启动目录

            // 定义要写入的文本
            string textToWrite = path;
            if (j == 0)
            // 使用StreamWriter类来写入文本，并设置Append为true以确保每次写入都另起一行
            {
                bool tf = listBox1.Items.Count == 1 ? false : true;
                using (StreamWriter sw = new StreamWriter(filePath, tf))
                {
                    sw.WriteLine(textToWrite);
                }

            }


            // 指定文件夹的路径
            //string folderPath = @"E:\Gitee项目\项目14_快速打开程序\快速打开程序";
            string folderPath = path;

            // 检查文件夹是否存在
            if (Directory.Exists(folderPath))
            {
                // 使用cmd执行git命令
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";
                startInfo.WorkingDirectory = folderPath;
                startInfo.Arguments = "/C git push -f origin master";
                startInfo.UseShellExecute = false;
                startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = true;
                using (Process process = new Process())
                {
                    process.StartInfo = startInfo;
                    process.Start();
                    // 可以读取命令的输出
                    string output = process.StandardOutput.ReadToEnd();
                    // 等待命令执行完成
                    process.WaitForExit();
                    // 可以在这里处理输出或者显示给用户
                    MessageBox.Show("强推成功", "git push", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                // 如果文件夹不存在，则显示错误消息
                MessageBox.Show("文件夹不存在，请检查路径是否正确。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }





            //打开文件夹
            /*
             // 打开文件夹的路径
            string folderPath = @"D:\Your\Folder\Path";
            // 检查文件夹是否存在
            if (Directory.Exists(folderPath))
            {
                // 使用Explorer打开文件夹
                Process.Start("explorer", folderPath);
             
             
             */
        }


        /// <summary>
        /// 删除分支
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            // 指定文件夹的路径
            string folderPath = @"E:\Gitee项目\项目3_WMS\WMS";
            // 检查文件夹是否存在
            if (Directory.Exists(folderPath))
            {
                // 使用cmd执行git命令
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";
                startInfo.WorkingDirectory = folderPath;
                startInfo.Arguments = "/C git branch -D dev";
                startInfo.UseShellExecute = false;
                startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = true;
                using (Process process = new Process())
                {
                    process.StartInfo = startInfo;
                    process.Start();
                    // 可以读取命令的输出
                    string output = process.StandardOutput.ReadToEnd();
                    // 等待命令执行完成
                    process.WaitForExit();
                    // 可以在这里处理输出或者显示给用户
                    MessageBox.Show("分支删除成功", "dev Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                // 如果文件夹不存在，则显示错误消息
                MessageBox.Show("文件夹不存在，请检查路径是否正确。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }





            //打开文件夹
            /*
             // 打开文件夹的路径
            string folderPath = @"D:\Your\Folder\Path";
            // 检查文件夹是否存在
            if (Directory.Exists(folderPath))
            {
                // 使用Explorer打开文件夹
                Process.Start("explorer", folderPath);
             
             
             */
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            string pathtxt = "Record.txt";

            if (File.Exists(pathtxt))
            {
                ReadTxtToLst(listBox1, pathtxt);
            }
            else
            {
                using (StreamWriter sw = new StreamWriter(pathtxt, false))
                {
                    sw.WriteLine("选择文件夹");
                }
                listBox1.Items.Add("选择文件夹");
            }

        }
        public void WriteLstToTxt(ListBox lst, string spath)  //listbox 写入txt文件
        {
            int count = lst.Items.Count;
            _wstream = new StreamWriter(spath);
            for (int i = 0; i < count; i++)
            {
                string data = lst.Items[i].ToString();
                _wstream.Write(data);
                _wstream.WriteLine();
            }
            _wstream.Flush();
            _wstream.Close();
        }


        public void ReadTxtToLst(ListBox lst, string spath)  //listbox 读取txt文件
        {
            _rstream = new StreamReader(spath, System.Text.Encoding.Default);
            string line;
            while ((line = _rstream.ReadLine()) != null)
            {
                lst.Items.Add(line);
            }
            _rstream.Close();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
